var app = getApp();
Page({
  data: {
    painting: {},
    shareImage: '',
    detail: '',
    userInfo: '',
    id: '',
    imgurl: ''
  },
  onLoad(options) {
    var _this = this;
    var user_id = options.id;

    _this.setData({
      user_id: user_id,
    })

    var openid = wx.getStorageSync('user_new');
    if (!openid) {
      wx.showModal({
        title: '提示',
        content: '本功能需要授权后才可使用，点击确定跳转到登录界面',
        success(res) {
          if (res.confirm) {
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/member/index',
              });
            }, 500)
          } else if (res.cancel) {
          }
        }
      })
      return;
    }
    app.core('get', 'aiCode', { user_id: user_id, flag: 2 }, (res) => {

      _this.setData({
        user: res.data,
        adviserInfo: res.adviserInfo,
        code: res.code,
        ai_bg: res.ai_bg,
        avatar_bg: res.avatar_bg,
      })

      _this.eventDraw()
    })

  },
  onShow() {
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  eventDraw() {
    wx.showLoading({
      title: 'AI名片中...',
      mask: true
    })
    var userinfomation = wx.getStorageSync('userInfo');
    var code = this.data.code;
    var ai_bg = this.data.ai_bg;
    var avatar_bg = this.data.avatar_bg;

    var adviserInfo = this.data.adviserInfo;

    this.setData({
      painting: {
        width: 375,
        height: 555,
        clear: true,
        views: [
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/img/ai_bg.jpg?v=1',
            top: 0,
            left: 0,
            width: 375,
            height: 555
          },
          {
            type: 'image',
            url: adviserInfo.headpic,
            top: 84,
            left: 230,
            width: 100,
            height: 100
          },
          {
            type: 'image',
            url: 'https://xkcloude.hfhouse.com/img/avatar_bg2.png?v=1',
            top: 84,
            left: 230,
            width: 100,
            height: 100
          },
          {
            type: 'text',
            content: userinfomation.nickName,
            fontSize: 16,
            color: '#333',
            textAlign: 'center',
            top: 170,
            left: 70,
            bolder: true
          },
          {
            type: 'text',
            content: (adviserInfo.wechatId ? adviserInfo.wechatId : ''),
            fontSize: 11,
            color: '#333',
            textAlign: 'left',
            top: 209,
            left: 96
          },
          {
            type: 'text',
            content: (adviserInfo.phone ?  adviserInfo.phone : ''),
            fontSize: 11,
            color: '#333',
            textAlign: 'left',
            top: 228,
            left: 96
          },
          {
            type: 'image',
            url: code,
            top: 374,
            left: 134,
            width: 110,
            height: 110
          }
        ]
      }
    })
  },
  eventSave() {
    var id = this.data.id;
    console.log(id)
    // console.log(this.data.shareImage)
    // let _this = this;
    wx.saveImageToPhotosAlbum({
      filePath: this.data.shareImage,
      success(res) {
        wx.showToast({
          title: '保存图片成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/member/index',
          });
        }, 1000)
      },
      fail(err) {
        if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
          // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  console.log("settingdata", settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限成功,再次点击图片即可保存',
                      showCancel: false,
                    })
                  } else {
                    wx.showModal({
                      title: '提示',
                      content: '获取权限失败，将无法保存到相册哦~',
                      showCancel: false,
                    })
                  }
                },
                fail(failData) {
                  console.log("failData", failData)
                },
                complete(finishData) {
                  console.log("finishData", finishData)
                }
              })
            }
          })
        }
      },
    })
  },
  eventGetImage(event) {
    console.log(event)
    wx.hideLoading()
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      this.setData({
        shareImage: tempFilePath
      })
    }
  },
  previewImg(e) {
    var current = e.target.dataset.src;
    console.log(current)
    wx.previewImage({
      current: current,
      urls: [this.data.shareImage]
    })
  },



  eventCodeImgSave(){
    var code = this.data.adviserInfo.code;
    wx.downloadFile({
      url: code, //仅为示例，并非真实的资源
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
              wx.showToast({
                title: '保存成功',
              })
          },
          fail(res){

          }
        })
      }
    })

  }
})
